package org.example.jida;

import cn.com.jit.assp.dsign.DSign;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 3.1.2（文件）Attach签名/（文件）Attach验签名(原型2) ok
 * 直接读取文件，再将加密后的数据保存在文件中
 * 会将源文件全部住转换成加密后的Base64类型的数据，无法直接查看，需要进行解密后才能才能获取其中的原文数据
 */
public class AttachSignFileTest {
    public static void main(String[] args) {
        DSign signDs = new DSign();
        signDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");  // 通过配置文件读取参数
        String strFileNameIn = "d:/src.txt";    // 待签名的文件路径
        String strFileNameOut = "d:/desrc.txt";  //签名后结果的保存路径
        /****************attachSign签名*******************************************/
        signDs.attachSign(signDs.getConfigObject().getAppID(), strFileNameIn, strFileNameOut);
        InputStream inputStream = null;
        int readNum = 0;
        try {
            inputStream = new FileInputStream(strFileNameOut);
            readNum = inputStream.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (readNum > 0) {
            System.out.println("文件签名成功！");
        } else {
            System.out.println("错误号为:" + signDs.getErrorCode());
            System.out.println("错误描述:" + signDs.getErrorMessage());
        }


        ///*****************verifyAttachedSign验签名*****************************/
        DSign vSignDs = new DSign();



        vSignDs.initConfig("E:/root/epolicy/epolicyModel/cssconfig/2.properties");
        long longReturn = 0;
        String strFileNameAttached = "d:/desrc.txt";
        longReturn = vSignDs.verifyAttachedSign(strFileNameAttached);
        if (longReturn == 0) {
            System.out.println("证书主题:" + vSignDs.getCertInfo("VS", 0, ""));
            System.out.println("颁发者主题:" + vSignDs.getCertInfo("VS", 1, ""));
            System.out.println("序列号:" + vSignDs.getCertInfo("VS", 2, ""));
            System.out.println("版本号:" + vSignDs.getCertInfo("VS", 3, ""));
            System.out.println("证书实体内容:" + vSignDs.getCertInfo("VS", 7, ""));
            //签名结果带时间戳
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new DateFormatSymbols(Locale.SIMPLIFIED_CHINESE));
            String signedTimeStr = "";
            if (vSignDs.getTsaTime() != null) {
                signedTimeStr = formatter.format(vSignDs.getTsaTime());
            }
            System.out.println("时间: " + signedTimeStr);
            System.out.println("证书subjectdn: " + vSignDs.getTsaCertInfo("subjectdn"));
            System.out.println("证书issure： " + vSignDs.getTsaCertInfo("issuerdn"));
            System.out.println("证书SN： " + vSignDs.getTsaCertInfo("serialnumber"));

            vSignDs.getFile("e:/test/");   //验签后文件保存在e盘的test目录下
        } else {
            System.out.println("错误号为:" + vSignDs.getErrorCode());
            System.out.println("错误描述:" + vSignDs.getErrorMessage());
        }
    }
}